package com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.globals;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSEnvironment;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSObject;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.global.BaseJSNativeFunction;

public class BaseJSConsole extends BaseJSObject {

	private static final BaseJSNativeFunction log = new BaseJSNativeFunction("log") {

		@Override
		public Object invoked(BaseJSEnvironment e, BaseJSNativeFunction f, final Object... variables) {
			if (variables == null || variables.length == 0)
				return null;
			if (variables.length == 1) {
				JVM.println(variables[0]);
			} else {
				JVM.println(Arrays.toString(variables));
			}
			return null;
		}

	};

	private static Map<String, BaseJSObject> OBJECTS = new ConcurrentHashMap<String, BaseJSObject>();
	static {
		if (OBJECTS.isEmpty()) {
			OBJECTS.put("log", log);
		}
	}

	public BaseJSConsole() {
		super("console", Collections.unmodifiableMap(OBJECTS));
	}
}
